What is load-bmfont?
The load-bmfont npm package is used to load bitmap font files in various formats (e.g., XML, JSON, binary). It is particularly useful for rendering text in WebGL or Canvas applications where bitmap fonts are preferred for performance reasons.
What are load-bmfont's main functionalities?
Load Bitmap Font from URL
This feature allows you to load a bitmap font from a URL. The callback function provides the loaded font object or an error if the loading fails.
const loadBMFont = require('load-bmfont');
loadBMFont('path/to/font.fnt', function(err, font) {
if (err) throw err;
console.log(font);
});
Load Bitmap Font from Buffer
This feature allows you to load a bitmap font from a buffer. This is useful when the font data is already available in memory, such as when it is read from a file or received over a network.
const loadBMFont = require('load-bmfont');
const fs = require('fs');
const buffer = fs.readFileSync('path/to/font.fnt');
loadBMFont(buffer, function(err, font) {
if (err) throw err;
console.log(font);
});
Load Bitmap Font from Base64 String
This feature allows you to load a bitmap font from a base64-encoded string. This can be useful for embedding font data directly within a script or HTML document.
const loadBMFont = require('load-bmfont');
const base64String = 'base64-encoded-font-data';
const buffer = Buffer.from(base64String, 'base64');
loadBMFont(buffer, function(err, font) {
if (err) throw err;
console.log(font);
});
Other packages similar to load-bmfont
three-bmfont-text
The three-bmfont-text package is used to create text geometry for Three.js using bitmap fonts. It provides a higher-level abstraction for rendering text in 3D scenes, whereas load-bmfont focuses on loading the font data itself.
fontpath
The fontpath package is a tool for working with vector fonts in JavaScript. While it does not specifically handle bitmap fonts, it provides similar functionality for loading and rendering vector fonts, which can be an alternative to bitmap fonts in some use cases.
load-bmfont
Loads an AngelCode BMFont file in browser (with XHR) and node (with fs and phin), returning a JSON representation.
var load = require("load-bmfont");
load("fonts/Arial-32.fnt", function (err, font) {
if (err) throw err;
console.log(font.common.lineHeight);
console.log(font.info);
console.log(font.chars);
console.log(font.kernings);
});
Currently supported BMFont formats:
- ASCII (text)
- JSON
- XML
- binary
See Also
See text-modules for related modules.
Usage
load(opt, cb)
Loads a BMFont file with the opt
settings and fires the callback with (err, font)
params once finished. If opt
is a string, it is used as the URI. Otherwise the options can be:
uri
or url
the path (in Node) or URIbinary
boolean, whether the data should be read as binary, default false- (in node) options for
fs.readFile
or phin - (in browser) options for xhr
To support binary files in the browser and Node, you should use binary: true
. Otherwise the XHR request might come in the form of a UTF8 string, which will not work with binary files. This also sets up the XHR object to override mime type in older browsers.
load(
{
uri: "fonts/Arial.bin",
binary: true,
},
function (err, font) {
console.log(font);
}
);
License
MIT, see LICENSE.md for details.